<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="168px">
            <el-form-item label="" prop="designCategory">
                <el-input v-model="queryParams.designCategory" placeholder="应用品类" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="designIntroduce">
                <el-input v-model="queryParams.designIntroduce" placeholder="面料完整介绍" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="" prop="designSpecialPresentation">
                <el-input v-model="queryParams.designSpecialPresentation" placeholder="面料卖点(针对总表)" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="designAttributeCategory">
                <el-input v-model="queryParams.designAttributeCategory" placeholder="属性类别" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="copywritingFamily">
                <el-input v-model="queryParams.copywritingFamily" placeholder="面料家族" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="copywritingFabricName">
                <el-input v-model="queryParams.copywritingFabricName" placeholder="面料昵称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="designFabricName">
                <el-input v-model="queryParams.designFabricName" placeholder="面料名称（产品）" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="designExerciseIntensity">
                <el-input v-model="queryParams.designExerciseIntensity" placeholder="运动强度" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="designFlexibility">
                <el-input v-model="queryParams.designFlexibility" placeholder="弹性" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="designFitnessLevels">
                <el-input v-model="queryParams.designFitnessLevels" placeholder="健身层级" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="designSomatosensory">
                <el-input v-model="queryParams.designSomatosensory" placeholder="体感" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="designSoftness">
                <el-input v-model="queryParams.designSoftness" placeholder="柔软度" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="designFabricThickness">
                <el-input v-model="queryParams.designFabricThickness" placeholder="面料厚度" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="designBreathability">
                <el-input v-model="queryParams.designBreathability" placeholder="透气度" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>



            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['product:Fabric:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['product:Fabric:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['product:Fabric:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['product:Fabric:export']">导出</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button size="mini" plain icon="el-icon-upload" @click="daoru">导入</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="FabricList" @selection-change="handleSelectionChange" @row-dblclick="t">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="面料属性ID" align="center" prop="id" /> -->
            <el-table-column label="应用品类" align="center" prop="designCategory" />

            <el-table-column width="150px" label="面料完整介绍" align="center" prop="designIntroduce">

                <template slot-scope="scope">
                    <el-popover trigger="hover" placement="bottom" width="300">
                        <!-- <span>{{ scope.row.designIntroduce }}</span> -->
                        <el-input v-model="scope.row.designIntroduce" :min-height="192" type="textarea"
                            :autosize="{ minRows: 2, maxRows: 24 }" :style="{ width: '100%' }" :disabled="true" />
                        <div slot="reference">
                            <span class="textname">

                                {{ scope.row.designIntroduce }}
                            </span>
                        </div>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column width="180px" prop="designSpecialPresentation" :label="'面料卖点(针对总表)'" align="center">
                <template slot-scope="scope">
                    <el-popover trigger="hover" placement="bottom" width="300">
                        <!-- <span>{{ scope.row.designSpecialPresentation }}</span> -->
                        <el-input v-model="scope.row.designSpecialPresentation" :min-height="192" type="textarea"
                            :autosize="{ minRows: 2, maxRows: 24 }" :style="{ width: '100%' }" :disabled="true" />
                        <div slot="reference">
                            <span class="textname">

                                {{ scope.row.designSpecialPresentation }}
                            </span>
                        </div>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column label="属性类别" align="center" prop="designAttributeCategory" />

            <el-table-column label="家族分类" align="center" prop="copywritingFamily" />
            <el-table-column width="100px" label="面料昵称" align="center" prop="copywritingFabricName" />
            <el-table-column width="130px" label="面料名称（产品）" align="center" prop="designFabricName" />
            <!-- <el-table-column label="特别呈现卖点" align="center" prop="designSpecialPresentation" /> -->

            <el-table-column label="运动强度" align="center" prop="designExerciseIntensity" />
            <el-table-column label="弹性" align="center" prop="designFlexibility" />
            <el-table-column label="健身层级" align="center" prop="designFitnessLevels" />
            <el-table-column label="体感" align="center" prop="designSomatosensory" />
            <el-table-column label="柔软度" align="center" prop="designSoftness" />
            <el-table-column label="面料厚度" align="center" prop="designFabricThickness" />
            <el-table-column label="透气度" align="center" prop="designBreathability" />



            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['product:Fabric:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['product:Fabric:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改面料属性信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
            <div class="el-dialog-div">
                <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                    <el-col :span="8">
                        <el-form-item label="应用品类" prop="designCategory">
                            <el-input v-model="form.designCategory" placeholder="应用品类" />
                        </el-form-item>
                        <el-form-item label="面料完整介绍" prop="designIntroduce">
                            <el-input v-model="form.designIntroduce" placeholder="面料完整介绍" type="textarea"
                                :autosize="{ minRows: 4, maxRows: 12 }" :style="{ width: '100%' }" />
                        </el-form-item>
                        <el-form-item label="面料卖点(针对总表)" prop="designSpecialPresentation">
                            <el-input v-model="form.designSpecialPresentation" placeholder="面料卖点(针对总表)" type="textarea"
                                :autosize="{ minRows: 4, maxRows: 12 }" :style="{ width: '100%' }" />
                        </el-form-item>
                        <el-form-item label="属性类别" prop="designAttributeCategory">
                            <el-input v-model="form.designAttributeCategory" placeholder="属性类别" />
                        </el-form-item>
                        <el-form-item label="家族分类" prop="copywritingFamily">
                            <el-input v-model="form.copywritingFamily" placeholder="家族分类" />
                        </el-form-item>
                        <el-form-item label="面料昵称" prop="copywritingFabricName">
                            <el-input v-model="form.copywritingFabricName" placeholder="面料昵称" />
                        </el-form-item>

                    </el-col>
                    <el-col :span="1">
                        <el-form-item></el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="面料名称（产品）" prop="designFabricName">
                            <el-input v-model="form.designFabricName" placeholder="面料名称" />
                        </el-form-item>
                        <el-form-item label="运动强度" prop="designExerciseIntensity">
                            <el-input v-model="form.designExerciseIntensity" placeholder="运动强度" />
                        </el-form-item>
                        <el-form-item label="弹性" prop="designFlexibility">
                            <el-input v-model="form.designFlexibility" placeholder="弹性" />
                        </el-form-item>
                        <el-form-item label="健身层级" prop="designFitnessLevels">
                            <el-input v-model="form.designFitnessLevels" placeholder="健身层级" />
                        </el-form-item>
                        <el-form-item label="体感" prop="designSomatosensory">
                            <el-input v-model="form.designSomatosensory" placeholder="体感" />
                        </el-form-item>
                        <el-form-item label="柔软度" prop="designSoftness">
                            <el-input v-model="form.designSoftness" placeholder="柔软度" />
                        </el-form-item>
                        <el-form-item label="面料厚度" prop="designFabricThickness">
                            <el-input v-model="form.designFabricThickness" placeholder="面料厚度" />
                        </el-form-item>
                        <el-form-item label="透气度" prop="designBreathability">
                            <el-input v-model="form.designBreathability" placeholder="透气度" />
                        </el-form-item>


                    </el-col>
                    <el-col :span="1">
                        <el-form-item></el-form-item>
                    </el-col>

                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </div>
        </el-dialog>
        <!-- 添加或修改产品二级信息对话框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" append-to-body>
            <el-upload :action="Upload.url" :headers="Upload.headers" :limit="limit" :on-exceed="handleExceed"
                :on-success="handlePictureSuccessPic" :before-upload="beforeAvatarUpload"
                :on-preview="handlePictureCardPreview" :file-list="fileLi" :on-change="handleChange">
                <el-button size="small" type="primary">导入文件</el-button>
                <!-- <i class="el-icon-plus"></i> -->

            </el-upload>

        </el-dialog>
    </div>
</template>
  
<script>
import { listFabric, getFabric, delFabric, addFabric, updateFabric } from "@/api/data/Fabric";
//--------导入token
import { getToken } from "@/utils/auth";
export default {
    name: "Fabric",
    data() {
        return {
            pjtUrl: process.env.VUE_APP_BASE_API,
            imgUpload: {
                // 设置上传的请求头部
                headers: {
                    Authorization: "Bearer " + getToken()
                },
                // 图片上传的方法地址:
                url: process.env.VUE_APP_BASE_API + "/common/upload",
            },
            //面料检测报告
            fabricTestRepor: '',
            // fileLi: [{ url: "" }],
            filefabricTestRepor: [],
            //导入
            dialogVisible: false,
            file: '',
            fileLi: [{ url: "" }],
            // 图片数量限制
            limit: 2,
            Upload: {
                // 设置上传的请求头部
                headers: {
                    Authorization: "Bearer " + getToken()
                },
                // 图片上传的方法地址:
                url: process.env.VUE_APP_BASE_API + "/product/Fabric/importDesignFabric",
            },
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 面料属性信息表格数据
            FabricList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                designFabricName: null,
                designSpecialPresentation: null,
                designExerciseIntensity: null,
                designFlexibility: null,
                designFitnessLevels: null,
                designSomatosensory: null,
                designSoftness: null,
                designFabricThickness: null,
                designBreathability: null,
                designFabricFunction: null,
                designCategory: null,
                designIntroduce: null,
                designAttributeCategory: null,
                copywritingFabricName: null,
                copywritingFamily: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                designFabricName: [
                    { required: true, message: "面料名称不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        //点击某一行
        t(row, column, cell, event) {
            console.log(row, column, cell, event);
            this.handleUpdate(row)
        },

        downLoadFile(value) {
            console.log(JSON.stringify(value));
            if (!value) {
                this.$modal.msgError('该货号没有上传附件，请选择至少一个文件！')
                return

            }

            window.location.href = this.pjtUrl + value;

            // download.resource(value)
            //根据文件路径参数，按斜杠进行分割，取得文件名，这是download函数需要的第三个参数
            let list = value.split("/");
            let fileName = list[list.length - 1];
            //这是文件路径参数，因为download函数需要传三个参数，这是第二个参数
            let params = {
                resource: value
            }
            /** request里面的download下载函数 */
            //download函数是若依自带的，第一个参数是请求的url路径，不需要变，这个路径下的controller后台方法也是若依写好封装好了的。
            // download("/common/download/resource", params, fileName);
            // console.log("文件名");
            // console.log("value");
        },
        daoru() {
            this.dialogVisible = true
        },
        /** 查询面料属性信息列表 */
        getList() {
            this.loading = true;
            listFabric(this.queryParams).then(response => {
                this.FabricList = response.rows;
                console.log(JSON.stringify(this.FabricList));
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                designFabricName: null,
                designSpecialPresentation: null,
                designExerciseIntensity: null,
                designFlexibility: null,
                designFitnessLevels: null,
                designSomatosensory: null,
                designSoftness: null,
                designFabricThickness: null,
                designBreathability: null,
                designFabricFunction: null,
                designCategory: null,
                designIntroduce: null,
                designAttributeCategory: null,
                copywritingFabricName: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加面料属性信息";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getFabric(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改面料属性信息";
                // this.fabricTestRepor = response.data.fabricTestRepor
            });
        },
        /** 提交按钮 */
        submitForm() {
            // this.form.fabricTestRepor = this.fabricTestRepor
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateFabric(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addFabric(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除面料属性信息编号为"' + ids + '"的数据项？').then(function () {
                return delFabric(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('product/Fabric/export', {
                ...this.queryParams
            }, `Fabric_${new Date().getTime()}.xlsx`)
        },
        //图片上传前的相关判断
        beforeAvatarUpload(file) {

            // const isJPG = file.type === 'image/jpeg' || file.type == 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 5;
            // if (!isJPG) {
            //     this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
            // }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 5MB!');
            }
            return isLt2M;
        },
        //图片预览
        handlePictureCardPreview(file) {
            // this.picUrl = file.url
            // this.imageUrl = file.url;
            // this.dialogVisible = true;
        },
        //上传文件让第二次覆盖第一的文件
        handleChange(file, fileList) {
            if (fileList.length > 0) {
                this.fileLi = [fileList[fileList.length - 1]]
            }
            // }
        },
        handlePictureSuccessPic(res, file) {
            console.log(JSON.stringify(res));
            //设置图片访问路径 （url 后台传过来的的上传地址）
            // this.picUrl = file.response.url;
            // const url = file.response.fileName;
            this.file = file.response.url;
            console.log(JSON.stringify(this.file));
            if (res.code == 200) {
                this.$modal.msgSuccess('导入成功')
                this.dialogVisible = false
                this.$modal.msgError(res.msg);
                this.getList();
                return true
            } else {
                this.$modal.msgError(res.msg);
                this.dialogVisible = true
                // this.$message.msgError('该颜色未在字典定义')
                return
            }

        },
        // //图片上传成功后的回调
        // handlePictureSuccess(res, file) {
        //     //设置图片访问路径 （url 后台传过来的的上传地址）
        //     this.imageUrl = file.response.url;
        //     // const url = file.response.fileName;
        //     this.logoUrl = file.response.fileName;
        //     // console.log(JSON.stringify(url));
        // },

        // 文件个数超出
        handleExceed() {
            this.$modal.msgError(`上传链接文件数量不能超过 ${this.limit} 个!`);
        },
        changeFile2(file, fileList) {
            if (fileList.length > 1) {
                this.filefabricTestRepor = fileList.slice(-1);

                // this.fileList = [fileList[fileList.length - 1]]//这一步，是 展示最后一次选择文件
            }
            // this.fileSpecial = fileList;
        },
        handlePictureSuccess(res, file) {
            //设置图片访问路径 （url 后台传过来的的上传地址）
            // this.picUrl = file.response.url;
            // // const url = file.response.fileName;
            // this.picurl = file.response.urle;
            this.fabricTestRepor = file.response.fileName;
            console.log(this.fabricTestRepor);

        },
    }
};
</script>
<style lang="scss" scoped>
span.textname {
    display: -webkit-box;

    text-overflow: ellipsis;
    overflow: hidden;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    white-space: pre-wrap;
}

.el-dialog-div {
    height: 80vh;
    overflow-x: hidden;
}

.el-input.is-disabled ::v-deep .el-input__inner {
    color: #606266;
    background-color: white;
    border: none;
}

.el-textarea.is-disabled ::v-deep .el-textarea__inner {
    color: #606266;
    background-color: white;
    border: none;
    width: 500px;
    resize: none;
    cursor: pointer;

}
</style>